package demo1.servlet.userBuy;

import com.alibaba.fastjson.JSON;
import demo1.pojo.PageBean;
import demo1.pojo.goods;
import demo1.pojo.users;
import demo1.service.goodsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/onSubmit")
public class onSubmit extends HttpServlet {//条件查询商品
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接受数据
        BufferedReader br = req.getReader();
        String params = br.readLine();//json字符串

        //转为user对象
        goods goods = JSON.parseObject(params, goods.class);

        String _currentPage = req.getParameter("currentPage");
        String _pageSize = req.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        goodsService goodsService = new goodsService();
        PageBean<goods> pageBean = null;
        try {
            pageBean = goodsService.onSubmit(currentPage,pageSize,goods);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        String jsonString = JSON.toJSONString(pageBean);
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
